﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace UniversalBlazor.CoreComponent
{
    /// <summary>
    /// 属性构建器
    /// </summary>
    public class PropertyStringBuilder : IEnumerable<string>
    {
        protected virtual string Separator { get; } = " ";
        private List<string> properties = new List<string>();
        /// <summary>
        /// 创建一个 <seealso cref="CssClassBuilder"/>
        /// </summary>
        /// <returns></returns>
        public static PropertyStringBuilder CreateCssClassBuilder()
        {
            return new CSSClassBuilder();
        }
        /// <summary>
        /// 创建一个 <seealso cref="CssStyleBuilder"/>
        /// </summary>
        /// <returns></returns>
        public static PropertyStringBuilder CreateCssStyleBuilder()
        {
            return new CSSStyleBuilder();
        }

        /// <summary>
        /// 如果满足条件，则添加一个属性
        /// </summary>
        /// <param name="condition">条件</param>
        /// <param name="propertyLists">css 属性</param>
        /// <returns></returns>
        public PropertyStringBuilder AddIf(bool condition, params string[] propertyLists)
        {
            if (!condition)
            {
                return this;
            }
            foreach (var propertyString in propertyLists)
            {
                if (propertyString == null)
                {
                    continue;
                }
                properties.AddRange(propertyString.Split(';').Select(x => x.Trim()).Where(x => !string.IsNullOrEmpty(x)));
            }
            return this;
        }

        /// <summary>
        /// 添加一个属性
        /// </summary>
        /// <param name="cssList">css 属性</param>
        /// <returns></returns>
        public PropertyStringBuilder Add(params string[] cssList)
        {
            return AddIf(true, cssList);
        }

        /// <summary>
        /// 移除一个属性
        /// </summary>
        /// <param name="styleName"></param>
        /// <returns></returns>
        public PropertyStringBuilder Remove(string styleName)
        {
            var stylePrefix = $"{styleName}:";
            properties.RemoveAll(x => x.StartsWith(stylePrefix, StringComparison.CurrentCultureIgnoreCase));
            return this;
        }

        internal string ToString(string sep)
        {
            return string.Join(sep, properties.Where(x => !string.IsNullOrWhiteSpace(x)));
        }

        public override string ToString()
        {
            return ToString(Separator);
        }

        public IEnumerator<string> GetEnumerator()
        {
            return new PropertyBuilderEnumerator(properties);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return new PropertyBuilderEnumerator(properties);
        }
        public static implicit operator string(PropertyStringBuilder b) => b.ToString();

        internal class PropertyBuilderEnumerator : IEnumerator<string>
        {
            private readonly List<string> properties;
            private string current;
            private int index = -1;
            public PropertyBuilderEnumerator(List<string> properties)
            {
                this.properties = properties;
            }
            public string Current => current;

            object IEnumerator.Current => current;

            public void Dispose()
            {

            }

            public bool MoveNext()
            {
                index++;
                if (properties.Count <= index)
                {
                    return false;
                }
                current = properties[index];
                return true;
            }

            public void Reset()
            {
                index = -1;
                current = null;
            }
        }
    }
}
